import { defineStore } from 'pinia'
import { ref } from 'vue'
import { aiApi } from '@/api/modules/ai'
import type { AISummary, AISummaryCreate } from '@/api/modules/ai'
import notify from '@/utils/notify'

/**
 * AI功能Store
 * 管理AI智能分析相关的状态和操作
 */
export const useAIStore = defineStore('ai', () => {
  // ========== State ==========
  const summaries = ref<AISummary[]>([])
  const currentSummary = ref<AISummary | null>(null)
  const loading = ref(false)
  const generating = ref(false) // AI生成中
  const extracting = ref(false) // 标签提取中

  // ========== Actions ==========

  /**
   * 创建AI总结
   * @param leadId 客户ID
   * @param data 创建数据
   */
  async function createSummary(leadId: number, data: AISummaryCreate) {
    generating.value = true
    try {
      const result = await aiApi.createSummary(leadId, data)
      summaries.value.unshift(result) // 添加到列表头部
      currentSummary.value = result
      notify.success('AI总结生成成功')
      return result
    } catch (error) {
      console.error('AI总结生成失败', error)
      notify.error('AI总结生成失败，请稍后重试')
      return null
    } finally {
      generating.value = false
    }
  }

  /**
   * 获取AI总结列表
   * @param leadId 客户ID
   */
  async function fetchSummaries(leadId: number) {
    loading.value = true
    try {
      const result = await aiApi.getSummaries(leadId)
      summaries.value = result
      return result
    } catch (error) {
      console.error('获取AI总结失败', error)
      notify.error('获取AI总结失败')
      return null
    } finally {
      loading.value = false
    }
  }

  /**
   * 批量提取AI标签
   * @param batchId 导入批次ID
   */
  async function extractBatchTags(batchId: string) {
    extracting.value = true
    try {
      const result = await aiApi.extractTags(batchId)
      const message = result.message || `AI标签提取完成: 成功${result.success}条，失败${result.failed}条`
      notify.success(message)
      return result
    } catch (error) {
      console.error('AI标签提取失败', error)
      notify.error('AI标签提取失败，请检查网络或稍后重试')
      return null
    } finally {
      extracting.value = false
    }
  }

  /**
   * 审核AI总结
   * @param summaryId 总结ID
   */
  async function reviewSummary(summaryId: number) {
    loading.value = true
    try {
      const result = await aiApi.reviewSummary(summaryId)
      // 更新列表中的记录
      const index = summaries.value.findIndex((s) => s.id === summaryId)
      if (index !== -1) {
        summaries.value[index] = result
      }
      notify.success('AI总结已标记为已审核')
      return result
    } catch (error) {
      console.error('审核AI总结失败', error)
      notify.error('审核AI总结失败')
      return null
    } finally {
      loading.value = false
    }
  }

  /**
   * 更新AI总结
   * @param summaryId 总结ID
   * @param text 新的总结文本
   */
  async function updateSummary(summaryId: number, text: string) {
    loading.value = true
    try {
      const result = await aiApi.updateSummary(summaryId, text)
      // 更新列表中的记录
      const index = summaries.value.findIndex((s) => s.id === summaryId)
      if (index !== -1) {
        summaries.value[index] = result
      }
      if (currentSummary.value?.id === summaryId) {
        currentSummary.value = result
      }
      notify.success('AI总结更新成功')
      return result
    } catch (error) {
      console.error('更新AI总结失败', error)
      notify.error('更新AI总结失败')
      return null
    } finally {
      loading.value = false
    }
  }

  /**
   * 清空当前状态
   */
  function clearState() {
    summaries.value = []
    currentSummary.value = null
    loading.value = false
    generating.value = false
    extracting.value = false
  }

  return {
    // State
    summaries,
    currentSummary,
    loading,
    generating,
    extracting,

    // Actions
    createSummary,
    fetchSummaries,
    extractBatchTags,
    reviewSummary,
    updateSummary,
    clearState,
  }
})
